local Map = require("core.Map")
local Obj = require("core.Obj")
local MapGrid = require("core.MapGrid")
--扫描单方阵营 type:0 中间阵营 type:1 敌对阵营 type:2 友方阵营
function scan1(obj, type, callback, conditionFunc, maxCount, userData)
    local view = MapGrid.get_view(obj)
    if not view then return end

    local camps = view[type]
    local len = camps[0]
    local i   = 0
    local index = 0
    local list  = nil
    local size  = 0
    local currCount = 0

    while len>i do

        list = camps[i+1]
        size = list[0]

        ::tag::
        if size > index then
            index = index + 1

            -- 获取OBJ
            local target = list[index]
            if target then  -- (这里可能为nil,因为可能在callback里可以让当前obj destroy，而会影响list表)
              if conditionFunc(obj, target, userData) then
                callback(obj, target, userData)

                -- 有扫描上限
                if maxCount then
                    currCount = currCount+1
                    if currCount >= maxCount then return  end
                end
              end
            end
            goto tag
        else
            i = i + 1
            index = 0
        end
    end
end


--扫描两方阵营 type:0 遍历 中间/敌方阵营    type:1 遍历 敌方/友方阵营
function scan2(obj, type, callback, conditionFunc, maxCount, userData)
    local view =  MapGrid.get_view(obj)
    if not view then return end

    local MAX_CAMPS = 2 + type
    local indexCamps = 0 + type
    local camps = nil

    local i   = 0
    local index = 0
    local list  = nil
    local size  = 0
    local len = 0

    local currCount = 0

    while MAX_CAMPS>indexCamps do

        ::tagNextCamps::
        camps = view[indexCamps]
        len = camps[0]

        if len<1 then
            indexCamps = indexCamps + 1
            if indexCamps>=MAX_CAMPS then return end
            goto tagNextCamps --阵营没人 跳到下一阵营
        end

        ::tagList::
        list = camps[i+1]
        size = list[0]

        ::tag::
        if size > index then
            index = index + 1

            -- 获取OBJ
            local target = list[index]
            if target then  -- (这里可能为nil,因为可能在callback里可以让当前obj destroy，而会影响list表)
              if conditionFunc(obj, target, userData) then
                callback(obj, target, userData)

                -- 有扫描上限
                if maxCount then
                    currCount = currCount+1
                    if currCount >= maxCount then return  end
                end
              end
            end

            goto tag
        else
            i = i + 1
            index = 0
        end

        if i>=len then
            i = 0
            indexCamps = indexCamps + 1
        else
            goto tagList
        end
    end
end

--扫描三方阵营
function scan3(obj, callback, conditionFunc, maxCount, userData)
    local view = MapGrid.get_view(obj)
    if not view then return end

    local MAX_CAMPS = 3
    local indexCamps = 0
    local camps = nil

    local i   = 0
    local index = 0
    local list  = nil
    local size  = 0
    local len = 0
    local currCount = 0

    while MAX_CAMPS>indexCamps do

        ::tagNextCamps::
        camps = view[indexCamps]
        len = camps[0]

        if len<1 then
            indexCamps = indexCamps + 1
            if indexCamps>=MAX_CAMPS then return end
            goto tagNextCamps --阵营没人 跳到下一阵营
        end

        ::tagList::
        list = camps[i+1]
        size = list[0]

        ::tag::
        if size > index then
            index = index + 1

            -- 获取OBJ
            local target = list[index]
            if target then  -- (这里可能为nil,因为可能在callback里可以让当前obj destroy，而会影响list表)
              local result = conditionFunc(obj, target, userData)
              if result then
                callback(obj, target, userData)

                -- 有扫描上限
                if maxCount then
                    currCount = currCount+1
                    if currCount >= maxCount then return end
                end
              end
            end

            goto tag
        else
            i = i + 1
            index = 0
        end

        if i>=len then
            i = 0
            indexCamps = indexCamps + 1
        else
            goto tagList
        end
    end
end

--遍历 中间阵营/友方阵营
function scan4(obj, callback, conditionFunc, maxCount, userData)
    local view = MapGrid.get_view(obj)
    if not view then return end

    local MAX_CAMPS = 3
    local indexCamps = 0
    local camps = nil

    local i   = 0
    local index = 0
    local list  = nil
    local size  = 0
    local len = 0
    local currCount = 0

    while MAX_CAMPS>indexCamps do

        ::tagNextCamps::
        camps = view[indexCamps]
        len = camps[0]

        if len<1 or indexCamps==1 then  --跳过敌方阵营   0:none 1:bad 2:good
            indexCamps = indexCamps + 1
            if indexCamps>=MAX_CAMPS then return end
            goto tagNextCamps --阵营没人 跳到下一阵营
        end

        ::tagList::
        list = camps[i+1]
        size = list[0]

        ::tag::
        if size > index then
            index = index + 1

            -- 获取OBJ
            local target = list[index]
            if target then  -- (这里可能为nil,因为可能在callback里可以让当前obj destroy，而会影响list表)
              if conditionFunc(obj, target, userData) then
                callback(obj, target, userData)

                -- 有扫描上限
                if maxCount then
                    currCount = currCount+1
                    if currCount >= maxCount then return  end
                end
              end
            end

            goto tag
        else
            i = i + 1
            index = 0
        end

        if i>=len then
            i = 0
            indexCamps = indexCamps + 1
        else
            goto tagList
        end
    end
end

function scanAreaLiveHuman(map_id, scene_id, areaid_list)
    local nHumanList = {}
    local nSum = 0
    for __, nAreaID in ipairs(areaid_list) do
        local list_rect = Map.getRects(map_id, nAreaID)
        if Obj.scenes[scene_id] then
            for _, v in pairs(Obj.scenes[scene_id]) do
                if v.obj_type == Obj.TYPE_HUMAN and not v.hide and v.db.hp > 0 then
                    for i = 1, #list_rect do
                        local rect = list_rect[i]
                        local min_x = rect[1]
                        local min_y = rect[2]
                        local max_x = rect[3]
                        local max_y = rect[4]
                        if min_x < v.x and v.x < max_x and min_y < v.y and v.y < max_y then
                            nSum = nSum + 1
                            nHumanList[v._id] = v
                        end
                    end
                end
            end
        end
    end
    return nSum, nHumanList
end
